package ide.ccf.ccproject.data;

import ide.ccf.ccproject.common.Utils;
import ide.ccf.ccproject.translate.Message;
import ide.ccf.ccproject.wizards.pages.ExportSelectFolderWizardPage;


import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;  
import java.io.IOException;  
import java.io.PrintWriter;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;


/**
 * 实现添加control.xml文件的功能
 *
 */
public class SetControlXml{
	protected Shell shell;  
	protected Object result;
	public Text tName, tGenericName, tSDescription, tVersion,
	tArch, tExec, tSDescription1, tLDescription,tLDescription1, 
	autohr_name, author_tel, author_page, author_email, author_description, author_description1,  tPage;
	Combo c_catrgory, c_dep, c_dense, cProtocol;
	Label sDescription, lDescription, sDescription1, lDescription1;
	Button r1, r2, is_Desktop, is_NDesktop, is_Patch, is_NPatch, is_Essential, is_NEssential;
	final Display display = Display.getDefault();
	public int flag = 0;
	private Document document;

	public SetControlXml(Shell parent) {
        try {
            DocumentBuilderFactory factory = DocumentBuilderFactory
                    .newInstance();
            DocumentBuilder builder = factory.newDocumentBuilder();
            this.document = builder.newDocument();
        } catch (ParserConfigurationException e) {
            System.out.println(e.getMessage());
        }
	}
	
	 public Object open(String path) { 
	        createContents();  
	        shell.open();  
	        shell.layout(); 
	        if(Utils.isExistFile(path))
	        	setInfoToPage(path);
	        while (!shell.isDisposed()) {  
	            if (!display.readAndDispatch())  
	                display.sleep();  
	        }  
	        return result;  
	    }  
	 
	public void close(){
		 shell.close();
	 }
	 
	 protected void createContents() {  
	        shell = new Shell(display, SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);  
	        shell.setSize(550, 650);  
	        shell.setText(Message.ExportSetInfoWizardPage_0);  
	        shell.setLayout(new FillLayout());
	        
	        final ScrolledComposite sc1 = new ScrolledComposite(shell, SWT.H_SCROLL  
	                | SWT.V_SCROLL | SWT.BORDER);  
	        final Composite c1 = new Composite(sc1, SWT.NONE);  
	        sc1.setContent(c1);  
	        GridLayout layout_ = new GridLayout();  
	        layout_.numColumns = 1;  
	        c1.setLayout(layout_);  
	        //基本信息(必填)
	        final Group g_info = new Group(c1, SWT.NONE);
	        g_info.setText("基本信息(必填)");
			GridData gd_info = new GridData(GridData.FILL_BOTH);
			gd_info.horizontalSpan = 2;
			g_info.setLayoutData(gd_info);
			GridLayout layout_info = new GridLayout(2,false);
			layout_info.verticalSpacing = 7;
			g_info.setLayout(layout_info);
			{
				Label name = new Label(g_info, SWT.NONE);
				name.setText("软件名称：");
				tName = new Text(g_info, SWT.BORDER);
				Label genericName = new Label(g_info, SWT.NONE);
				genericName.setText("通用名称：");
				tGenericName = new Text(g_info, SWT.BORDER);		
				Label category = new Label(g_info, SWT.NONE);
				category.setText("软件类别：");
				 c_catrgory = new Combo(g_info, SWT.NONE); 
				 c_catrgory.setItems(new String[]{"办公", "应用", "网络", "通讯",
						 "图像", "影音", "安全", "系统", "开发", "其他"});
				 c_catrgory.setText("办公");
				 Label version = new Label(g_info, SWT.NONE);
				 version.setText("软件版本：");
				 tVersion = new Text(g_info, SWT.BORDER);
				 Label arch = new Label(g_info, SWT.NONE);
				 arch.setText("软件架构：");
				 tArch = new Text(g_info, SWT.BORDER);
				 Label dep = new Label(g_info, SWT.NONE);
				 dep.setText("CCF依赖：");
				 c_dep = new Combo(g_info, SWT.NONE);
				 c_dep.setItems(new String[]{"CCF1(>=1.0)", "CCF1(>=2.0)"});
				 c_dep.setText("CCF1(>=1.0)");
				 Label dense = new Label(g_info, SWT.NONE);
				 dense.setText("软件密级：");
				 c_dense = new Combo(g_info, SWT.NONE);
				 c_dense.setItems(new String[]{"公开", "秘密", "机密", "绝密", "其他"});
				 c_dense.setText("公开");
				 Label exec = new Label(g_info, SWT.NONE);
				 exec.setText("执行语句：");
				 tExec = new Text(g_info, SWT.BORDER);
			}    
			
			//应用信息
			final Group g_details = new Group(c1, SWT.NONE);
			g_details.setText("应用信息");
			GridData gd_details = new GridData(GridData.FILL_BOTH);
			gd_details.horizontalSpan = 1;
			g_details.setLayoutData(gd_details);
			GridLayout layout_details = new GridLayout(1,false);
			layout_details.verticalSpacing = 1;
			g_details.setLayout(layout_details);
			final Composite c_1,c_2;
		   {
				c_1 = new Composite(g_details, SWT.NONE); 
			    GridLayout layout_1 = new GridLayout(2,false);  
			    layout_1.verticalSpacing = 7;
		        c_1.setLayout(layout_1); 
		        
				sDescription = new Label(c_1, SWT.NONE);
			    sDescription.setText(" 短描述中：");
				tSDescription = new Text(c_1, SWT.BORDER);
				sDescription1 = new Label(c_1, SWT.NONE);
				sDescription1.setText(" 短描述英：");
				tSDescription1 = new Text(c_1, SWT.BORDER);
				
				c_2 = new Composite(g_details, SWT.NONE); 
			    GridLayout layout_2 = new GridLayout(2,false);  
			    layout_2.verticalSpacing = 10;
			    c_2.setLayout(layout_2);
		    	lDescription = new Label(c_2, SWT.NONE);
				lDescription.setText("长描述中：");
				tLDescription = new Text(c_2, SWT.BORDER |SWT.MULTI | SWT.WRAP);
		        GridData gdText = new GridData(SWT.FILL, SWT.FILL, false, false, 1, 1);
		        gdText.heightHint = 100;
		        tLDescription.setLayoutData(gdText);
				lDescription1 = new Label(c_2, SWT.NONE);
				lDescription1.setText("长描述英：");
				tLDescription1 = new Text(c_2, SWT.BORDER |SWT.MULTI | SWT.WRAP);
		        GridData gdText1 = new GridData(SWT.FILL, SWT.FILL, false, false, 1, 1);
		        gdText1.heightHint = 100;
		        tLDescription1.setLayoutData(gdText1);
			}  
		   
		   //作者信息
			final Group g_author = new Group(c1, SWT.NONE);
			g_author.setText("作者信息");
			GridData gd_author = new GridData(GridData.FILL_BOTH);
			gd_author.horizontalSpan = 1;
			g_author.setLayoutData(gd_author);
			GridLayout layout_author = new GridLayout(2,false);
			layout_author.verticalSpacing = 7;
			g_author.setLayout(layout_author);
			{
				Label category = new Label(g_author, SWT.NONE);
				category.setText("作者类型：");
				Composite c_3 = new Composite(g_author, SWT.NONE);
				GridLayout layout_category = new GridLayout(2,false);
				layout_category.verticalSpacing = 7;
				c_3.setLayout(layout_category);
				r1 = new Button(c_3, SWT.RADIO);
				r1.setText("企业开发者");
				r2 = new Button(c_3, SWT.RADIO);
				r2.setText("个人开发者");
				r1.setSelection(true);
				Label name = new Label(g_author, SWT.NONE);
				name.setText("作者名称：");
				autohr_name = new Text(g_author, SWT.BORDER);
				Label tel = new Label(g_author, SWT.NONE);
				tel.setText("联系电话：");
				author_tel = new Text(g_author, SWT.BORDER);
				Label url = new Label(g_author, SWT.NONE);
				url.setText("主　　页：");
				author_page = new Text(g_author, SWT.BORDER);
				Label email = new Label(g_author, SWT.NONE);
				email.setText("电子邮箱：");
				author_email = new Text(g_author, SWT.BORDER);
				Label des_Zh = new Label(g_author, SWT.NONE);
				des_Zh.setText("打包描述中：");
				author_description = new Text(g_author, SWT.BORDER );
				Label des_En = new Label(g_author, SWT.NONE);
				des_En.setText("打包描述英：");
				author_description1 = new Text(g_author, SWT.BORDER );
			}
			
			//其他信息
			final Group g_other = new Group(c1, SWT.NONE);
			g_other.setText("其他信息");
			GridData gd_other = new GridData(GridData.FILL_BOTH);
			gd_other.horizontalSpan = 1;
			g_other.setLayoutData(gd_other);
			GridLayout layout_other = new GridLayout(2,false);
			layout_other.verticalSpacing = 7;
			g_other.setLayout(layout_other);
			Composite c_4, c_5, c_6;
			{
				Label is_desktop = new Label(g_other, SWT.NONE);
				is_desktop.setText("是否桌面应用：");
				c_4 = new Composite(g_other, SWT.NONE);
				GridLayout layout_desktop = new GridLayout(2,false);
				layout_desktop.verticalSpacing = 7;
				c_4.setLayout(layout_desktop);
				is_Desktop = new Button(c_4, SWT.RADIO);
				is_Desktop.setText("是");
				is_NDesktop = new Button(c_4, SWT.RADIO);
				is_NDesktop.setText("否");
				is_Desktop.setSelection(true);
				Label is_patch = new Label(g_other, SWT.NONE);
				is_patch.setText("是否桌面补丁：");
				c_5 = new Composite(g_other, SWT.NONE);
				GridLayout layout_patch = new GridLayout(2,false);
				layout_patch.verticalSpacing = 7;
				c_5.setLayout(layout_patch);
				is_Patch = new Button(c_5, SWT.RADIO);
				is_Patch.setText("是");
				is_NPatch = new Button(c_5, SWT.RADIO);
				is_NPatch.setText("否");
				is_NPatch.setSelection(true);
				Label is_essential = new Label(g_other, SWT.NONE);
				is_essential.setText("是否系统必须：");
				c_6 = new Composite(g_other, SWT.NONE);
				GridLayout layout_essential  = new GridLayout(2,false);
				layout_essential .verticalSpacing = 7;
				c_6.setLayout(layout_essential );
				is_Essential = new Button(c_6, SWT.RADIO);
				is_Essential.setText("是");
				is_NEssential = new Button(c_6, SWT.RADIO);
				is_NEssential.setText("否");
				is_NEssential.setSelection(true);
				Label protocol = new Label(g_other, SWT.NONE);
				protocol.setText("开源协议：");
				cProtocol = new Combo(g_other, SWT.NONE);
				cProtocol.setItems(new String[]{"GPL", "BSD", "MIT", "Distributable", "Commercial",
						"Share"});
				cProtocol.setText("GPL");
				Label page = new Label(g_other, SWT.NONE);
				page.setText("软件主页：");
				tPage  = new Text(g_other, SWT.BORDER);				
			}	
			
	        Composite s = new Composite(c1, SWT.NONE);
	        GridData gd_s = new GridData(GridData.FILL_BOTH);
			s.setLayoutData(gd_s);
			GridLayout layout_s= new GridLayout(4,false);
			layout_s.verticalSpacing = 7;
			layout_s.horizontalSpacing = 80;
			s.setLayout(layout_s);
			Label occupy1 = new Label(s, SWT.NONE);
			occupy1.setText("");
			Label occupy2 = new Label(s, SWT.NONE);
			occupy2.setText("");
			Button s_1  = new Button(s, SWT.PUSH);
			s_1.setText("取  消");
			s_1.addSelectionListener(new SelectionAdapter()
			{
				@Override
				public void widgetSelected(SelectionEvent e) {
					close();
					flag = 0;
				}
			});
			Button s_2 = new Button(s, SWT.PUSH);
			s_2.setText("生  成");
			s_2.addSelectionListener(new SelectionAdapter()
			{
				@Override
				public void widgetSelected(SelectionEvent e) {	
			        String str=ExportSelectFolderWizardPage.sfoldernamePath_cache + "/CPK/control.xml";
			        Utils.generalCPK(ExportSelectFolderWizardPage.sfoldernamePath_cache);
			        createXml(str);    //创建xml
			        close();
			        flag = 1;
				}
			});
		
	        c1.setSize(c1.computeSize(500, SWT.DEFAULT));  
			c_1.setSize(c_1.computeSize(500, SWT.DEFAULT));  
			c_2.setSize(c_2.computeSize(500, SWT.DEFAULT));  
			s.setSize(s.computeSize(500, SWT.DEFAULT));  
			
	        tName.setSize(tName.computeSize(300, 20));
	        tGenericName.setSize(tGenericName.computeSize(300, 20));
	        tSDescription.setSize(tSDescription.computeSize(300, 20));
	        tSDescription1.setSize(tSDescription1.computeSize(300, 20));
	        c_catrgory.setSize(c_catrgory.computeSize(310, 20));
	        tVersion.setSize(tVersion.computeSize(300, 20));
	        tArch.setSize(tArch.computeSize(300, 20));
	        c_dep.setSize(c_dep.computeSize(310, 20));
	        c_dense.setSize(c_dense.computeSize(310, 20));
	        tExec.setSize(tExec.computeSize(300, 20));
	        tLDescription.setSize(tLDescription.computeSize(320, 100));
	        tLDescription1.setSize(tLDescription1.computeSize(320,100));
	        autohr_name.setSize(autohr_name.computeSize(300, 20));
	        author_tel.setSize(author_tel.computeSize(300, 20));
	        author_page.setSize(author_page.computeSize(300, 20));
	        author_email.setSize(author_email.computeSize(300, 20));
	        author_description.setSize(author_description.computeSize(300, 20));
	        author_description1.setSize(author_description.computeSize(300, 20));
	        cProtocol.setSize(cProtocol.computeSize(310, 20));
	        tPage.setSize(tPage.computeSize(300, 20));
			s_1.setSize(s_1.computeSize(100, 30));
			s_2.setSize(s_2.computeSize(100, 30));
			occupy1.setSize(occupy1.computeSize(120, 30));
			occupy2.setSize(occupy2.computeSize(120, 30));
	    }  
	 
	 /**
	  * 生成xml文件
	  */
	public void createXml(String fileName) {
		 	Element root = this.document.createElement("CpkInfo");
	        this.document.appendChild(root);
	        Element software = this.document.createElement("Software");
	        software.setAttribute("name", tName.getText());
	        Element id = this.document.createElement("id");
	        software.appendChild(id);
	        Element genericname = this.document.createElement("genericname");
	        if(is_Desktop.getSelection() && !is_NDesktop.getSelection())
	        	genericname.setAttribute("type", "desktop");
	        else
	        	genericname.setAttribute("type", "");
	        Element keyword_en  = this.document.createElement("keyword");
	        keyword_en.setAttribute("lang", "en");
	        keyword_en.appendChild(this.document.createTextNode(tName.getText()));
	        genericname.appendChild(keyword_en);
	        Element keyword_zh  = this.document.createElement("keyword");
	        keyword_zh.setAttribute("lang", "zh");
	        keyword_zh.appendChild(this.document.createTextNode(tGenericName.getText()));
	        genericname.appendChild(keyword_zh);
	        software.appendChild(genericname);
	        Element permisson = this.document.createElement("permission");
	        permisson.appendChild(this.document.createTextNode("ccf.permission.USE_ROOT"));
	        software.appendChild(permisson);
	        Element classification = this.document.createElement("classification");
	        String strClassification = "L0";
	        switch(c_dense.getSelectionIndex())
	        {
	        	case 0:
	        	{
	        		strClassification = "L0";
	        		break;
	        	}
	        	case 1:
	        	{
	        	    strClassification = "L1";		
	        	    break;
	        	}
	        	case 2:
	        	{
	        	    strClassification = "L2";		
	        	    break;
	        	}
	        	case 3:
	        	{
	        	    strClassification = "L3";		
	        	    break;
	        	}
	        	case 4:
	        	{
	        	    strClassification = "L4";		
	        	    break;
	        	}
	        	default:
	        		break;
	        }
	        classification.appendChild(this.document.createTextNode(strClassification));
	        software.appendChild(classification);
	        Element is_patch = this.document.createElement("is_patch");
	        if(is_Patch.getSelection() && !is_NPatch.getSelection())
	        	is_patch.appendChild(this.document.createTextNode("1"));
	        else
	        	is_patch.appendChild(this.document.createTextNode("0"));
	        software.appendChild(is_patch);
	        Element summary = this.document.createElement("summary");
	        Element keyword_En  = this.document.createElement("keyword");
	        keyword_En.setAttribute("lang", "en");
	        keyword_En.appendChild(this.document.createTextNode(tSDescription1.getText()));
	        summary.appendChild(keyword_En);
	        Element keyword_Zh  = this.document.createElement("keyword");
	        keyword_Zh.setAttribute("lang", "zh");
	        keyword_Zh.appendChild(this.document.createTextNode(tSDescription.getText()));
	        summary.appendChild(keyword_Zh);
	        software.appendChild(summary);
	        Element description  = this.document.createElement("description");
	        Element keyword_eN  = this.document.createElement("keyword");
	        keyword_eN.setAttribute("lang", "en");
	        keyword_eN.appendChild(this.document.createTextNode(tLDescription1.getText()));
	        description.appendChild(keyword_eN);
	        Element keyword_zH  = this.document.createElement("keyword");
	        keyword_zH.setAttribute("lang", "zh");
	        keyword_zH.appendChild(this.document.createTextNode(tLDescription.getText()));
	        description.appendChild(keyword_zH);
	        software.appendChild(description);
	        Element version = this.document.createElement("version");
	        version.appendChild(this.document.createTextNode(tVersion.getText()));
	        software.appendChild(version);
	        Element essential = this.document.createElement("essential");
	        if(is_Essential.getSelection() && ! is_NEssential.getSelection())
	        	essential.appendChild(this.document.createTextNode("1"));
	        else
	        	essential.appendChild(this.document.createTextNode("0"));
	        software.appendChild(essential);
	        Element license = this.document.createElement("license");
	        license.appendChild(this.document.createTextNode(cProtocol.getText()));
	        software.appendChild(license);
	        Element category = this.document.createElement("category");
	        String strCategory = "Office";
	        switch(c_catrgory.getSelectionIndex())
	        {
	        	case 0:
	        	{
	        		strCategory = "Office";
	        		break;
	        	}
	        	case 1:
	        	{
	        		strCategory = "Application";
	        		break;
	        	}
	        	case 2:
	        	{
	        		strCategory = "Network";
	        		break;
	        	}
	        	case 3:
	        	{
	        		strCategory = "IM";
	        		break;
	        	}
	        	case 4:
	        	{
	        		strCategory = "Graphics";
	        		break;
	        	}
	        	case 5:
	        	{
	        		strCategory = "Mutimedia";
	        		break;
	        	}
	        	case 6:
	        	{
	        		strCategory = "Security";
	        		break;
	        	}
	        	case 7:
	        	{
	        		strCategory = "System";
	        		break;
	        	}
	        	case 8:
	        	{
	        		strCategory = "Development";
	        		break;
	        	}
	        	case 9:
	        	{
	        		strCategory = "Other";
	        		break;
	        	}
	        	
	        }
	        category.appendChild(this.document.createTextNode(strCategory));
	        software.appendChild(category);
	        Element architecture = this.document.createElement("architecture");
	        architecture.appendChild(this.document.createTextNode(tArch.getText()));
	        software.appendChild(architecture);
	        Element exec = this.document.createElement("exec");
	        exec.appendChild(this.document.createTextNode(tExec.getText()));
	        software.appendChild(exec);
	        Element install = this.document.createElement("install");
	        install.appendChild(this.document.createTextNode(tGenericName.getText()+".install"));//TODO mencius
	        software.appendChild(install);
	        Element homepage = this.document.createElement("homepage");
	        homepage.appendChild(this.document.createTextNode(tPage.getText()));
	        software.appendChild(homepage);
	        Element vendor = this.document.createElement("vendor");
	        if(r1.getSelection() && !r2.getSelection())
	        	vendor.setAttribute("type", "company");
	        else
	        	vendor.setAttribute("type", "personal");
	        Element name = this.document.createElement("name");
	        name.appendChild(this.document.createTextNode(autohr_name.getText()));
	        vendor.appendChild(name);
	        Element telephone = this.document.createElement("telephone");
	        telephone.appendChild(this.document.createTextNode(author_tel.getText()));
	        vendor.appendChild(telephone);
	        Element email = this.document.createElement("email");
	        email.appendChild(this.document.createTextNode(author_email.getText()));
	        vendor.appendChild(email);
	        Element url = this.document.createElement("url");
	        url.appendChild(this.document.createTextNode(author_page.getText()));
	        vendor.appendChild(url);
	        Element description_ = this.document.createElement("description");
	        Element keyword_EN  = this.document.createElement("keyword");
	        keyword_EN.setAttribute("lang", "en");
	        keyword_EN.appendChild(this.document.createTextNode(author_description1.getText()));//TODO mencius
	        description_.appendChild(keyword_EN);
	        Element keyword_ZH  = this.document.createElement("keyword");
	        keyword_ZH.setAttribute("lang", "zh");
	        keyword_ZH.appendChild(this.document.createTextNode(author_description.getText()));//TODO mencius
	        description_.appendChild(keyword_ZH);
	        vendor.appendChild(description_);
	        software.appendChild(vendor);
	        Element pkgs_count = this.document.createElement("pkgs_count");
	        pkgs_count.appendChild(this.document.createTextNode(""));
	        software.appendChild(pkgs_count);
	        Element pkgs = this.document.createElement("pkgs");
	        Element dateline = this.document.createElement("dateline");
	        dateline.appendChild(this.document.createTextNode(""));
	        pkgs.appendChild(dateline);
	        Element self_size = this.document.createElement("self_size");
	        self_size.appendChild(this.document.createTextNode(""));
	        pkgs.appendChild(self_size);
	        Element inst_size = this.document.createElement("inst_size");
	        inst_size.appendChild(this.document.createTextNode(""));
	        pkgs.appendChild(inst_size);
	        Element depends = this.document.createElement("depends");
	        if(c_dep.getText().equals("CCF1(>=1.0)"))
	        	depends.appendChild(this.document.createTextNode("ccf1 (>= 1.0.0)"));
	        else if(c_dep.getText().equals("CCF1(>=2.0)"))
	        	depends.appendChild(this.document.createTextNode("ccf1 (>= 2.0.0)"));
	        pkgs.appendChild(depends); 
	        software.appendChild(pkgs);
	        Element source_ = this.document.createElement("source");
	        source_.appendChild(this.document.createTextNode(""));
	        software.appendChild(source_);
	        root.appendChild(software);
	        TransformerFactory tf = TransformerFactory.newInstance();
	        try {
	            Transformer transformer = tf.newTransformer();
	            DOMSource source = new DOMSource(document);
	            transformer.setOutputProperty(OutputKeys.ENCODING, "utf-8");
	            transformer.setOutputProperty(OutputKeys.INDENT, "yes");
	            PrintWriter pw = new PrintWriter(new FileOutputStream(fileName));
	            StreamResult result = new StreamResult(pw);
	            transformer.transform(source, result);
	            System.out.println("生成XML文件成功!");
	        } catch (TransformerConfigurationException e) {
	            System.out.println(e.getMessage());
	        } catch (IllegalArgumentException e) {
	            System.out.println(e.getMessage());
	        } catch (FileNotFoundException e) {
	            System.out.println(e.getMessage());
	        } catch (TransformerException e) {
	            System.out.println(e.getMessage());
	        }
	}
	
	/**
	 * 解析xml文件
	 * @param fileName
	 * @param key
	 * @return
	 */
	public static String  parserXml(String fileName, String key) {
		try {
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder();
            Document document = db.parse(fileName);             
            NodeList employees = document.getChildNodes();
            for (int i = 0; i < employees.getLength(); i++) {
                Node employee = employees.item(i);
                NodeList employeeInfo = employee.getChildNodes();

                for (int j = 0; j < employeeInfo.getLength(); j++) {
                    Node node = employeeInfo.item(j);
                    if (node.getNodeName().equals("Software") && key.equals("name"))
                    {
                    	String strName = node.getAttributes().getNamedItem("name").getNodeValue();
                    	return strName;
                    }
                    
                    NodeList employeeMeta = node.getChildNodes();
                    for (int k = 0; k < employeeMeta.getLength(); k++ ) {
                    	Node node2 = employeeMeta.item(k);
                        if(node2.getNodeName().equals("genericname") && (key.equals("genericname_Type") ||
                        		key.equals("genericname")))
                        {
/*                            System.out.println(node2.getNodeName()
                                    + ":" + node2.getNodeName());*/
                        	if(key.equals("genericname_Type"))
                        		return node2.getAttributes().getNamedItem("type").getNodeValue();
                    		 NodeList employeeMeta2 = node2.getChildNodes();
                    		 for (int s = 0; s < employeeMeta2.getLength(); s++) {
                    			 Node node3= employeeMeta2.item(s);
                    			 if(node3.getAttributes() != null)
                    			 {
 /*                                System.out.println("node3.getNodeName()===1======="
                                             + ":" + node3.getNodeName()); 
                                     System.out.println(" node3.getAttributes()..getNodeValue()=====1===="
                                    		 + ":" +  node3.getAttributes().getNamedItem("lang").getNodeValue()); */
                        			 if(key.equals("genericname") && 
                        					 node3.getAttributes().getNamedItem("lang").getNodeValue().equals("zh"))
                        			 {
  /*                                       System.out.println("node3.getNodeName()===2======="
                                                 + ":" + node3.getNodeName()); 
                                         System.out.println(" node3.getAttributes()..getNodeValue()=====2===="
                                                 + ":" +  node3.getAttributes().getNamedItem("lang").getNodeValue()); */
                                         return node3.getTextContent();
                        			 }
                    			 }
                    		 }       	
                        }
                        else  if(node2.getNodeName().equals("summary") && (key.equals("summaryEn") ||
                        		key.equals("summaryZh")))
                        {	
  /*                      	System.out.println(node2.getNodeName()
                                    + ":" + node2.getTextContent());*/
                    		 NodeList employeeMeta2 = node2.getChildNodes();
                    		 for (int s = 0; s < employeeMeta2.getLength(); s++) {
                    			 Node node3= employeeMeta2.item(s);
                    			 if(node3.getAttributes() != null)
                    			 {
/*                    				 System.out.println("1=:"+node3.getAttributes().getNamedItem("lang").getNodeValue());
                        			 System.out.println("2=:"+ node3.getTextContent());*/
                        			 if(key.equals("summaryEn") &&
                        					 node3.getAttributes().getNamedItem("lang").getNodeValue().equals("en"))
                        				 return node3.getTextContent();
                        			 else if( key.equals("summaryZh") &&
                        					 node3.getAttributes().getNamedItem("lang").getNodeValue().equals("zh"))
                        				 return node3.getTextContent();
                    			 }
                    		 }       	
                        }
                        else if(node2.getNodeName().equals("description") && (key.equals("descriptionEn") || key.equals("descriptionZh")))
                        {
/*                        	System.out.println(node2.getNodeName()
                                    + ":" + node2.getTextContent());*/
                    		 NodeList employeeMeta2 = node2.getChildNodes();
                    		 for (int s = 0; s < employeeMeta2.getLength(); s++) {
                    			 Node node3= employeeMeta2.item(s);
                    			 if(node3.getAttributes() != null && node3.getAttributes().getNamedItem("lang") != null) 
                    			 {
                        			 if(key.equals("descriptionEn") && 
                        					 node3.getAttributes().getNamedItem("lang").getNodeValue().equals("en"))
                        				 return node3.getTextContent();
                        			if(key.equals("descriptionZh") &&
                        					node3.getAttributes().getNamedItem("lang").getNodeValue().equals("zh"))
                        					 return node3.getTextContent();
                    			 }
                    		 }  
                        }
                        else if(node2.getNodeName().equals("vendor") && (key.equals("vendor_Type") || key.equals("vendor_Name")
                        		|| key.equals("vendor_Tel") || key.equals("vendor_Email") || key.equals("vendor_Url") ||
                        		key.equals("vendor_DescriptionEn") || key.equals("vendor_DescriptionZh")))
                        {
/*                        	System.out.println(node2.getNodeName()
                                    + ":" + node2.getTextContent());*/
                        	if( key.equals("vendor_Type"))
                        		return node2.getAttributes().getNamedItem("type").getTextContent();
                    		 NodeList employeeMeta2 = node2.getChildNodes();
                    		 for (int s = 0; s < employeeMeta2.getLength(); s++) {
                    			 Node node3= employeeMeta2.item(s);
                    			if(node3.getNodeName().equals("name") && key.equals("vendor_Name"))
                    				return node3.getTextContent();
                    			if(node3.getNodeName().equals("telephone") && key.equals("vendor_Tel"))
                    				return node3.getTextContent();
                    			if(node3.getNodeName().equals("email") && key.equals("vendor_Email"))
                    				return node3.getTextContent();
                    			if(node3.getNodeName().equals("url") && key.equals("vendor_Url"))
                    				return node3.getTextContent();
                    			if(node3.getNodeName().equals("description"))
                    			{
/*                    				System.out.println(node3.getNodeName()
                                            + ":" + node3.getTextContent());*/
                    				 NodeList employeeMeta3 = node3.getChildNodes();
                            		 for (int a = 0; a < employeeMeta3.getLength(); a++) {
                            			 Node node4= employeeMeta3.item(a);
                            			 if(node4.getAttributes() != null)
                            			 {
                                			 if(key.equals("vendor_DescriptionEn") &&
                                					 node4.getAttributes().getNamedItem("lang").getNodeValue().equals("en"))
                                				 return node4.getTextContent();
                                			 if( key.equals("vendor_DescriptionZh") &&
                                					 node4.getAttributes().getNamedItem("lang").getNodeValue().equals("zh"))
                                				 return node4.getTextContent();
                            			 }
                            		 }  
                    			}
                    		 }       	
                        }
                        else   if(node2.getNodeName().equals("pkgs") && key.equals("pkgs_Depends"))
                        {
/*                        	System.out.println(node2.getNodeName()
                                    + ":" + node2.getTextContent());*/
                     		 NodeList employeeMeta2 = node2.getChildNodes();
                    		 for (int s = 0; s < employeeMeta2.getLength(); s++) {
                    			 Node node3= employeeMeta2.item(s);
                    			if(node3.getNodeName().equals("depends") && key.equals("pkgs_Depends"))
                    				return node3.getTextContent();
                    		 }
                         }
                        else if(node2.getNodeName().equals(key))
                        	return node2.getTextContent();
                    }
                }
            }
            System.out.println("解析完毕");
        } catch (FileNotFoundException e) {
            System.out.println(e.getMessage());
        } catch (ParserConfigurationException e) {
            System.out.println(e.getMessage());
        } catch (SAXException e) {
            System.out.println(e.getMessage());
        } catch (IOException e) {
            System.out.println(e.getMessage());
        }
		return "";
	}

	public void setInfoToPage(String path)
	{
		System.out.println("path:"+path);
		String name = parserXml(path, "name");
		String version = parserXml(path, "version");
    	String arch = parserXml(path, "architecture");
		String genericName = parserXml(path, "genericname");
		String classification = parserXml(path, "classification");
		String isPatch = parserXml(path, "is_patch");
		String essential = parserXml(path, "essential");
		String license = parserXml(path, "license");
		String category = parserXml(path, "category");
		String exec = parserXml(path, "exec");
		String install = parserXml(path, "install");
		String homepage = parserXml(path, "homepage");
		String permission = parserXml(path, "permission");
		String genericname_Type = parserXml(path, "genericname_Type");
		String summaryEn = parserXml(path, "summaryEn");
		String summaryZh = parserXml(path, "summaryZh");
		String descriptionEn = parserXml(path, "descriptionEn");
		System.out.println("descriptionEn:"+ descriptionEn);
		String descriptionZh = parserXml(path, "descriptionZh");
		System.out.println("descriptionZh:"+ descriptionZh);
		String vendor_Type = parserXml(path, "vendor_Type");
		String vendor_Name = parserXml(path, "vendor_Name");
		String vendor_Tel = parserXml(path, "vendor_Tel");
		String vendor_Email = parserXml(path, "vendor_Email");
		String vendor_Url = parserXml(path, "vendor_Url");
		String vendor_DescriptionEn = parserXml(path, "vendor_DescriptionEn");
		String vendor_DescriptionZh = parserXml(path, "vendor_DescriptionZh");
		String pkgs_Depends = parserXml(path, "pkgs_Depends");

		tName.setText(name);
		tVersion.setText(version);
		tGenericName.setText(genericName);
		tArch.setText(arch);
		if(category.equals("Office"))  
			c_catrgory.setText("办公");
		else if(category.equals("Application"))
			c_catrgory.setText("应用");
		else if(category.equals("Network"))
			c_catrgory.setText("网络");
		else if(category.equals("IM"))
			c_catrgory.setText("通讯");
		else if(category.equals("Graphics"))
			c_catrgory.setText("图像");
		else if(category.equals("Mutimedia"))
			c_catrgory.setText("影音");
		else if(category.equals("Security"))
			c_catrgory.setText("安全");
		else if(category.equals("System"))
			c_catrgory.setText("系统");
		else if(category.equals("Development"))
			c_catrgory.setText("开发");
		else if(category.equals("Other"))
			c_catrgory.setText("其他");
		tExec.setText(exec);
		tPage.setText(homepage);
		if(pkgs_Depends.equals("ccf1 (>= 1.0.0)"))
			c_dep.setText("CCF1(>=1.0)");
		else if(pkgs_Depends.equals("ccf1 (>= 2.0.0)"))
			c_dep.setText("CCF1(>=2.0)");
		if(classification.equals("L0"))
			c_dense.setText("公开");
		else if(classification.equals("L1"))
			c_dense.setText("秘密");
		else if(classification.equals("L2"))
			c_dense.setText("机密");
		else if(classification.equals("L3"))
			c_dense.setText("绝密");
		tLDescription.setText(descriptionZh);
		tLDescription1.setText(descriptionEn);
		tSDescription.setText(summaryZh);
		tSDescription1.setText(summaryEn);
		if(isPatch.equals("1"))
		{
			is_Patch.setSelection(true);
			is_NPatch.setSelection(false);
		}
		else
		{
			is_Patch.setSelection(false);
			is_NPatch.setSelection(true);
		}
		if(vendor_Type.equals("personal"))
		{
			r2.setSelection(true);
			r1.setSelection(false);
		}
		else
		{
			r2.setSelection(false);
			r1.setSelection(true);
		}
		autohr_name.setText(vendor_Name);
		author_tel.setText(vendor_Tel);
		author_email.setText(vendor_Email);
		author_page.setText(vendor_Url);
		author_description.setText(vendor_DescriptionZh);
		author_description1.setText(vendor_DescriptionEn);
		if(essential.equals("1"))
		{
			is_Essential.setSelection(true);
			is_NEssential.setSelection(false);
		}
		else
		{
			is_Essential.setSelection(false);
			is_NEssential.setSelection(true);
		}
		cProtocol.setText(license);
		if(genericname_Type.equals("desktop"))
		{
			is_Desktop.setSelection(true);
			is_NDesktop.setSelection(false);
		}
		else
		{
			is_Desktop.setSelection(false);
			is_NDesktop.setSelection(true);
		}
	}
}
